Requirement:

For sending mail you need an outgoing mail server (that, in the case of this script, also needs to allow unauthenticated outgoing communication). Fill out the required credentials in the folowing variables:


In [ ]:
MAIL_SERVER = "mail.****.com"
FROM_ADDRESS = "noreply@****.com"
TO_ADDRESS = "my_friend@****.com"

Sending a mail is, with the proper library, a piece of cake...


In [1]:
from sender import Mail
mail = Mail(MAIL_SERVER)
mail.fromaddr = ("Secret admirer", FROM_ADDRESS)


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-f01e1e29ea29> in <module>()
----> 1 from sender import Mail
      2 mail = Mail(MAIL_SERVER)
      3 mail.fromaddr = ("Secret admirer", FROM_ADDRESS)

ImportError: No module named sender

In [ ]:
mail.send_message("Raspberry Pi has a soft spot for you", to=TO_ADDRESS, body="Hi sweety! Grab a smoothie?")

... but if we take it a little further, we can connect our doorbell project to the sending of mail!

APPKEY is the Application Key for a (free) http://www.realtime.co/ "Realtime Messaging Free" subscription.
See "104 - Remote deurbel - Een cloud API gebruiken om berichten te sturen" voor meer gedetailleerde info. info.


In [ ]:
APPKEY = "******"

In [ ]:
mail.fromaddr = ("Your doorbell", FROM_ADDRESS)

mail_to_addresses = {
    "Donald Duck":"dd@****.com",
    "Maleficent":"mf@****.com",
    "BigBadWolf":"bw@****.com"
    }

def on_message(sender, channel, message):
    mail_message = "{}: Call for {}".format(channel, message)
    print(mail_message)
    mail.send_message("Raspberry Pi alert!", to=mail_to_addresses[message], body=mail_message)

In [ ]:
import ortc
oc = ortc.OrtcClient()
oc.cluster_url = "http://ortc-developers.realtime.co/server/2.1"

def on_connected(sender):
    print('Connected')
    oc.subscribe('doorbell', True, on_message)
 
oc.set_on_connected_callback(on_connected)
oc.connect(APPKEY)

In [ ]: